//
//  LZDateFormatterManager.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/11/29.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZDateFormatterManager.h"

@interface LZDateFormatterManager ()

@property (nonatomic ,strong) NSDateFormatter *df;

@end

@implementation LZDateFormatterManager

+ (instancetype)shareInstance {
    static LZDateFormatterManager *lzDateFormatterManager;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lzDateFormatterManager = [[LZDateFormatterManager alloc] init];
    });
    
    return lzDateFormatterManager;
}

- (NSDateFormatter*)df {
    if (_df) {
        return _df;
    }
    
    _df = [[NSDateFormatter alloc] init];
    return _df;
}

// 获取普通的 yyyy-MM-dd 类型的日期字符串==
// 传参是 NSDate
- (NSString *)stringFromDateNormalHaveLineyyyyMMdd:(NSDate*)date {

    [self.df setDateFormat:@"dd-MM-yyyy"];
    return [self.df stringFromDate:date];
}

// 传参是 NSString
// 获取普通的 HH:mm 类型的日期字符串
- (NSString *)stringFromStringNormalHaveLineHHmm:(NSString*)aString {
    if (aString.length == 0) {
        return @"";
    }
    [self.df setDateFormat:@"HH:mm"];
    NSDate *date = [self.df dateFromString:aString];
    
    return [self.df stringFromDate:date];
}

// 传参是 NSString ==
// 获取普通的 yyyy-MM 类型的日期字符串
- (NSString *)stringFromStringNormalHaveLineyyyyMM:(NSString*)aString {
    if (aString.length == 0) {
        return @"";
    }
    [self.df setDateFormat:@"yyyy-MM"];
    NSDate *date = [self.df dateFromString:aString];
    [self.df setDateFormat:@"MM-yyyy"];
    
    return [self.df stringFromDate:date];
}

// 传参是 NSString ==
// 获取普通的 yyyy-MM-dd 类型的日期字符串
- (NSString *)stringFromStringNormalHaveLineyyyyMMdd:(NSString*)aString {
    if (aString.length == 0) {
        return @"";
    }
    [self.df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [self.df dateFromString:aString];
    [self.df setDateFormat:@"dd-MM-yyyy"];
    return [self.df stringFromDate:date];
}


// 传参是 NSString ==
// 获取普通的 yyyy-MM-dd HH:mm:ss 类型的日期字符串
- (NSString *)stringFromStringYMDHMSHaveLineyyyyMMddHHmmss:(NSString*)aString {
    if (aString.length == 0) {
        return @"";
    }
    [self.df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [self.df dateFromString:aString];
    [self.df setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
    return [self.df stringFromDate:date];
}

// 获取自定义类型的日期字符串
- (NSString *)stringFromCustomFormat:(NSString*)format andDate:(NSDate*)date {
    [self.df setDateFormat:format];
    return [self.df stringFromDate:date];
    
}

//返回格式 yyyy.mm.dd ==
- (NSString *)stringFromDoubleTimestampHavePointyyyyMMdd:(NSTimeInterval)timestamp
{
    if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
        [self.df setDateFormat:@"yyyy.MM.dd"];
    }
    else {
        [self.df setDateFormat:@"dd.MM.yyyy"];
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    NSString *strDate = [self.df stringFromDate:date];
    
    return strDate;
    
}

//返回格式 yyyy_mm_dd
- (NSString *)stringFromDoubleTimestampToyyyyLineMMLinedd:(NSTimeInterval)timestamp
{
    [self.df setDateFormat:@"dd-MM-yyyy"];

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    NSString *strDate = [self.df stringFromDate:date];
    
    return strDate;
}

- (NSString *)returnRemainingHMTimeWithEndTime:(NSTimeInterval)timestamp
{
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    
    long temp = timestamp/1000 - currentTime;
    if (temp < 0) {
        return @"0";
    }
    NSInteger iMinutes = (temp / 60) % 60;
    NSInteger iHours = (temp / 3600);
    
    return [NSString stringWithFormat:@"%ld:%ld",(long)iHours,iMinutes];
}

@end
